# -*- coding:utf-8 -*-
import easyocr
import time

import cv2
import numpy as np
from PIL import Image

# 可信度最小阈值
BASE_MIN = 0.1
BASE_ROTA = 0
time1 = time.time()
url = './6.jpg'
reader = easyocr.Reader(['ch_sim','en'],gpu=True)

result = reader.readtext(url,batch_size =100,rotation_info=[90,180,270],low_text=0.4,slope_ths=0.3,beamWidth =1,min_size=50)

numLis = [0,0,0,0]
for i in result:
    numLis[i[3]] = numLis[i[3]]+1
for i in range(len(numLis)):
    if(numLis[i]>numLis[BASE_ROTA]):
        BASE_ROTA = i
for i in result:
    if(float(i[2])>BASE_MIN and i[3] == BASE_ROTA):
        print(i[1])


image = cv2.imread(url)

# 灰度图
img=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化
ret , thresh = cv2.threshold(img, 230, 255, cv2.THRESH_BINARY_INV)

contours, hierarchy = cv2.findContours(cv2.Canny(img,80,100),cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)


dot=[]  # 用来保存所有轮廓返回的坐标点。
for c in contours:
    # 找到边界坐标
    min_list=[] # 保存单个轮廓的信息，x,y,w,h,area。 x,y 为起始点坐标
    x, y, w, h = cv2.boundingRect(c)  # 计算点集最外面的矩形边界
    min_list.append(x)
    min_list.append(y)
    min_list.append(w)
    min_list.append(h)
    min_list.append(w*h) # 把轮廓面积也添加到 dot 中
    dot.append(min_list)

max_area=dot[0][4]
for inlist in dot:
    area=inlist[4]
    if area >= max_area:
        x=inlist[0]
        y=inlist[1]
        w=inlist[2]
        h=inlist[3]
        max_area=area
# 在原图上画出最大的矩形
cv2.rectangle(image, (x, y), (x + w , y + h ), (0, 255, 0), 1)

cv2.imwrite('big.jpg',image)

img = Image.open('big.jpg')
region = img.crop((x, y, x + w, y + h))

if(BASE_ROTA == 1):
    region = region.transpose(Image.ROTATE_90)
elif(BASE_ROTA == 2):
    region = region.transpose(Image.ROTATE_180)
elif(BASE_ROTA == 3):
    region = region.transpose(Image.ROTATE_270)
region.save('result.jpg')
time2 = time.time()

print("绘制结束，用时：",time2-time1)